package com.lantosec.hmrp.modules.supplier.controller.app;


import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.core.mvc.BaseController;
import com.lantosec.hmrp.core.properties.P;
import com.lantosec.hmrp.core.util.UUIDUtils;
import com.lantosec.hmrp.modules.supplier.entity.SupplierAccessory;
import com.lantosec.hmrp.modules.supplier.service.ISupplierAccessoryService;
import com.lantosec.hmrp.modules.supplier.vo.SupplierAccessoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 *
 * 供应商附件信息 控制器
 *
 * @author AutoCode
 * @since 2020-02-20
 */
@Api(value = "供应商附件信息控制器APP端", tags = "供应商附件信息控制器APP端")
@RestController
@RequestMapping("/supplierAccessoryApp")
@Log4j2
public class SupplierAccessoryAppController extends BaseController {
    @Autowired
    private ISupplierAccessoryService service;


    @GetMapping("querySupplierAccessory")
    @ApiOperation(value = "根据附件类型查询")
    public Result<List<SupplierAccessoryVO>> querySupplierAccessory(@ApiParam(value = "supplierCode") @RequestParam(name="supplierCode") String supplierCode,
                                                                    @ApiParam(value = "accessoryType") @RequestParam(name="accessoryType",required = false) String accessoryType){
        List<SupplierAccessoryVO> list = service.querySupplierAccessory(supplierCode,accessoryType);
        return Result.ok(list);
    }

    @PostMapping("insertSupplierAccessory")
    @ApiOperation(value = "新增附件数据")
    public Result<String> create(@ApiParam(value = "供应商附件信息VO对象", required = true) @RequestBody @Validated List<SupplierAccessoryVO> voList){
        voList.forEach(parm->{
            parm.setAccessoryId(UUIDUtils.get());
            service.save(parm.convertToEntity());
        });
        return Result.ok();
    }

    @PutMapping("updateSupplierAccessory")
    @ApiOperation(value = "修改数据")
    public Result<Void> updateSupplierAccessory(@ApiParam(value = "供应商附件信息VO对象", required = true) @RequestBody SupplierAccessoryVO vo){
        SupplierAccessory entity = vo.convertToEntity();
        service.updateById(entity);
        return Result.ok();
    }

    @DeleteMapping(value = "deleteSupplierAccessory/{ids}")
    @ApiOperation("逻辑删除")
    public Result<Void> deleteSupplierAccessory(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name="ids") String ids){
        if(StringUtils.isNotEmpty(ids)){
            service.removeByIds(Arrays.asList(ids.split(",")));
        }
        return Result.ok();
    }
}
